/** 
 * Reply.java
 * @author Lex
 * 2007-9-2 ����11:12:26
 */
package org.lex.net.ftp;

import java.util.ArrayList;
import java.util.Iterator;

import org.lex.net.uitl.NetUtilities;
import org.lex.utils.SystemProperties;


/**
 * FTP Server Response
 */
public class FTPResponse implements Iterable<String> {
	private int replyCode = -1;
	private ArrayList<String> contents = null;

	/**
	 * a package-private constructor, this class shouldn't be constructed by
	 * users
	 */
	FTPResponse() {
		replyCode = -1;
		this.contents = new ArrayList<String>();
	}

	void addLine(String line) {
		this.contents.add(line);
	}

	public boolean hasReplyCode() {
		return -1 != this.getReplyCode();
	}

	public int getReplyCode() {
		if (-1 == replyCode) {
			if (this.contents.size() > 0)
				this.replyCode = NetUtilities.parseReplyCode(contents.get(0));
		}
		return replyCode;
	}

	public Iterator<String> iterator() {
		return contents.iterator();
	}

	public void remove() {
		throw new UnsupportedOperationException();
	}

	public String toString() {
		StringBuilder buf = new StringBuilder();
		int size = this.contents.size();
		String nl = SystemProperties.lineSeparator();
		for (int i = 0; i < size; i++) {
			buf.append(this.contents.get(i));
			if (i < size - 1)
				buf.append(nl);
		}
		return buf.toString();
	}
}
